<?php
require_once ("../src/db/connectdatabase.php");

function getRowRateCard($bank, $cardtype, $cardlevel) {
	$con = new ConnectDB();
	
	$sql = "SELECT COUNT(*) FROM carddetail WHERE bankid LIKE '%".$bank."%' AND cardtypeid LIKE '%".$cardtype."%' AND idcardlevel LIKE '%".$cardlevel."%'";
	return $con -> getvalueString($sql);
}

function getRateCards($bank, $cardtype, $cardlevel, $start, $lenght) {
	$con = new ConnectDB();
	
	$sql = "SELECT carddetailid, bankid, bankname, banklogo, balancetransfer, monthstransfer, interestrate, annualfee, cardtypeid, cardtypename, cardtypelogo, interestfreedays, latepaymentfee, minimumrepayment, idcardlevel, cardlevelname, datecreate, isuse FROM carddetail WHERE bankid LIKE '%".$bank."%' AND cardtypeid LIKE '%".$cardtype."%' AND idcardlevel LIKE '%".$cardlevel."%' ORDER BY datecreate DESC LIMIT ".$start.",".$lenght;
	return $con -> getvalueString($sql);
}



$bank = "";
$cardtype = "";
$cardlevel = "";

if(isset($_GET['bank'])) {
	$bank = $_GET['bank'];
}

if(isset($_GET['cty'])) {
	$cardtype = $_GET['cty'];
}

if(isset($_GET['cle'])) {
	$cardlevel = $_GET['cle'];
}

$page = 1;
if(isset($_GET['page'])) {
	$page = $_GET['page'];
}

$arrcountrows = getRowRateCard($bank, $cardtype, $cardlevel);

$sumrecord = $arrcountrows[0][0];
$numrecord = 50;
$pageNumber = intval(($sumrecord / $numrecord));
if(($sumrecord % $numrecord) != 0) {
	$pageNumber++;
}

if($pageNumber == 0) {
	$pageNumber = 1;	
}

if($page > $pageNumber) {
	$page = $pageNumber;
}

$arrdata = getRateCards($bank, $cardtype, $cardlevel, ($page-1)*$numrecord, $numrecord);
?>
<style>
.searchforlaisuat, .formlaisuatthecontent {
	width: 850px;
	margin-bottom: 5px;
}
.searchforlaisuat select {
	width: 170px;
	height: 30px;
	padding: 5px;
	margin-left: 5px;
	margin-right: 5px;
}

.formlaisuatthecontent table {
	width: 900px;
	max-width: 900px;
}

.formlaisuatthecontent table td {
	margin-top: 2px;
	border: 1px solid;
	border-radius: 2px 2px 2px 2px;
	padding: 2px;
}

.title {
	text-align: center;
	background-color: #0088cc;
	min-height: 40px;
	-moz-border-radius: 8px 8px 0px 0px;
	-webkit-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	padding: 3px;
	color: #FFF;
}

.formlaisuatthecontent table tr:hover td {
	background: #FFC;
}

.class-pagination{
		float: right;
		clear: left;
		padding-top: 20px;
	}
	
	.class-pageLabel{
		font-size: 11px;
		font-weight: bold;
		text-transform: uppercase;
	}

	.class-pageNow{
		padding: 5px;
		padding-top: 4px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		border: 1px solid #C0C0C0;
		background-color: #C0C0C0;
		border-radius: 4px;
		box-shadow: 0 0 10px rgba(0,0,0,.1);
		cursor: default;
	}
	
	.class-pageElem{
		padding: 5px;
		padding-top: 4px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		border: 1px solid #C0C0C0;
		box-shadow: 0 0 10px rgba(0,0,0,.2);
		border-radius: 4px;
		background-color: white;
	}

	.class-pageElem:hover{
		padding: 5px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		background-color: #C0C0C0;
		cursor: pointer;
	}
	.submitform {
		background: #004A00;
		text-indent: 1px;
		width:80px;
		height:30px;
		cursor: pointer;
		font-weight: bold;
		font-size: 14px;
	}
</style>
<h3>So Sánh Lãi Suất Thẻ Tín Dụng</h3>
<div class="searchforlaisuat">
<form action="" method="get">
	<h4 style="color:#03F; margin-bottom: -10px;">Lọc Theo:</h4><br>
	<label for="selectbank">Ngân Hàng</label>
	<select name="selectbank" class="bankloadselect">
    	<option value="">----Chọn Ngân Hàng----</option>
    </select>
	<label for="selectcardtype">Loại Thẻ</label>
	<select name="selectcardtype" class="selectcardtype">
    	<option value="">----Chọn Loại Thẻ----</option>
    </select>
    <label for="selectcardlevel">Card Level</label>
	<select name="selectcardlevel" class="selectcardlevel">
    	<option value="">----Chọn Card Level----</option>
    </select>
    <input type="button" id="form_submitsearch" name="submit" value="Lọc" style="border-radius: 5px; padding:5px; width:50px; background:#03F; color:#FFF; font-weight:bold;" />
</form>
</div>
<div style="clear:both; height:15px;"></div>
<div style="float:left; width: 300px;"><a href="admin.php?content=quanlythetindung&&as=add-laisuat-the"><img src="../images/icon-add.jpg" title="Thêm So Sánh Vay Lãi Suất" />Thêm So Sánh Vay Lãi Suất</a></div>
    <div style="float:right; width: 600px; text-align: right;">
        <strong>Chú Thích:</strong><img src="../images/unclockicon.png" title="Được Phép Hiển Thị" /> Hiển Thị &nbsp;&nbsp;<img src="../images/lockicon.png" title="Ẩn" /> Ẩn
    </div>
    <div style="clear:both;"></div>
<div class="class-pagination">
    <span class="class-pageLabel">Trang</span>
    <?php if($page == 1) { ?>
    <span title="Trang 1" page="1" class="class-pageNow">1</span>
    <?php } else { ?>
    <span title="Trang 1" page="1" class="class-pageElem">1</span>
    <?php }?>
    <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
    <?php if($i == $page) { ?>
    <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
    <?php } else { ?>
    <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
    <?php }?>
    <?php }}?>
</div>
<div style="clear:both; height:10px;"></div>
<div class="formlaisuatthecontent">
	<table>
    	<tr>
        	<th class="title" width="40px">STT</th>
        	<th class="title" width="70px">Ngân Hàng</th>
            <th class="title" width="90px">Chuyển Khoản Số Dư</th>
            <th class="title" width="60px">Lãi Suất</th>
            <th class="title" width="90px">Phí Hàng Năm</th>
            <th class="title" width="80px">Loại Thẻ</th>
            <th class="title" width="70px">Ngày Free</th>
            <th class="title" width="90px">Phí Chậm Thanh Toán</th>
            <th class="title" width="90px">Trả Nợ Tối Thiểu</th>
            <th class="title" width="80px">Card Level</th>
            <th class="title">Chức Năng</th>
        </tr>
        <?php
		$i = ($page-1)*$numrecord + 1;
		foreach($arrdata as $entry) {
		?>
        <tr>
        	<td align="center"><?php echo($i) ?></td>
        	<td align="center"><img src="../<?php echo($entry[3]) ?>" width="60px" alt="<?php echo($entry[2]) ?>(<?php echo($entry[1]) ?>)" title="<?php echo($entry[2]) ?>(<?php echo($entry[1]) ?>)" /></td>
            <td align="center"><strong><?php echo($entry[4]) ?>%</strong><br><span style="font-size:12px; color: #666;">(<?php echo($entry[5]) ?> Tháng)</span></td>
            <td align="center"><?php echo($entry[6]) ?>%</td>
            <td align="center"><?php echo($entry[7]) ?></td>
            <td align="center"><img src="../<?php echo($entry[10]) ?>"  width="70px" alt="<?php echo($entry[9]) ?>" title="<?php echo($entry[9]) ?>" /></td>
            <td align="center"><?php echo($entry[11]) ?></td>
            <td align="center"><?php echo($entry[12]) ?></td>
            <td align="center"><?php echo($entry[13]) ?></td>
            <td align="center"><?php echo($entry[15]) ?></td>
            <td align="center"><?php if($entry[17] == "1") { ?><img class="class-display-link" idpr="<?php echo($entry[0]) ?>" style="cursor:pointer;" src="../images/unclockicon.png" title="Click để không cho phép hiển thị" /><?php } else { ?><img class="class-hiden-link" idpr="<?php echo($entry[0]) ?>" style="cursor:pointer;" src="../images/lockicon.png" title="Click để hiển thị trên trang chủ" /><?php } ?>  <a href="admin.php?content=quanlythetindung&&as=edit-laisuat-the&&id=<?php echo($entry[0]) ?>"><img src="../images/icon_detail_cardiscount.png" title="Cập Nhật Thông Tin" /></a></td>
        </tr>
        <?php
			$i++;
		}
		?>
    </table>
</div>
<div style="clear:both;"></div>
<div class="class-pagination">
    <span class="class-pageLabel">Trang</span>
    <?php if($page == 1) { ?>
    <span title="Trang 1" page="1" class="class-pageNow">1</span>
    <?php } else { ?>
    <span title="Trang 1" page="1" class="class-pageElem">1</span>
    <?php }?>
    <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
    <?php if($i == $page) { ?>
    <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
    <?php } else { ?>
    <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
    <?php }?>
    <?php }}?>
</div>
<div style="clear:both;"></div>
<div style="float:left; width: 300px;"><a href="admin.php?content=quanlythetindung&&as=add-laisuat-the"><img src="../images/icon-add.jpg" title="Thêm So Sánh Vay Lãi Suất" />Thêm So Sánh Vay Lãi Suất</a></div>
<div style="clear:both;"></div>
<script>
$(document).ready(function(e) {
	loadbank();
	loadcardtype();
	loadcardlevel();
	
    function loadbank() {
		$.ajax({
			url: "../src/ajax/ajaxbank.php",
			type: "post",
			data: {act:'getbanks'},
			dataType:"json",
			success: function(response) {
				html = '';
				$.each(response, function(entryIndex, entry){
					html += '<option value="' + entry[0] + '">' + entry[1] + ' (' + entry[0] + ')</option>';
				});
				$('.bankloadselect').append(html);
				$(".bankloadselect option[value='<?php echo($bank) ?>']").attr('selected', 'selected');
			}
		});
	}
	
	function loadcardtype() {
		$.ajax({
			url: "../src/ajax/ajaxcardtype.php",
			type: "post",
			data: {act:'getall'},
			dataType:"json",
			success: function(response) {
				html = '';
				$.each(response, function(entryIndex, entry){
					html += '<option value="' + entry[0] + '">' + entry[1] + '</option>';
				});
				$('.selectcardtype').append(html);
				$(".selectcardtype option[value='<?php echo($cardtype) ?>']").attr('selected', 'selected');
			}
		});
	}
	
	function loadcardlevel() {
		$.ajax({
			url: "../src/ajax/ajaxcardlevel.php",
			type: "post",
			data: {act:'getall'},
			dataType:"json",
			success: function(response) {
				html = '';
				$.each(response, function(entryIndex, entry){
					html += '<option value="' + entry[0] + '">' + entry[1] + '</option>';
				});
				$('.selectcardlevel').append(html);
				$(".selectcardlevel option[value='<?php echo($cardlevel) ?>']").attr('selected', 'selected');
			}
		});
	}
	
	$('#form_submitsearch').click(function(e) {
		var bank = $('.bankloadselect').val();
		var cty = $('.selectcardtype').val();
		var cle = $('.selectcardlevel').val();
        openwindowns('admin.php?content=quanlythetindung&&as=laisuatthe&bank=' + bank + '&cty=' + cty + '&cle=' + cle);
    });
	
	$(document).on('click', '.class-pageElem', function() {
		page = $(this).attr('page');
		var bank = $('.bankloadselect').val();
		var md = $('#form_search_mucdich').val();
		var cp = $('#form_search_bank').val();
        openwindowns('admin.php?content=quanlythetindung&&as=laisuatthe&bank=' + bank + '&cty=' + cty + '&cle=' + cle + '&page=' + page);
	});
	
	function openwindowns(url) {
		window.location = url;
	}
	
	$(document).on('click', '.class-display-link', function() {
		elem = $(this);
		id = elem.attr('idpr');
		$.ajax({
			type: "POST",
			url: "../src/ajax/ajaxadmincarddetail.php",
			data:{act: 'edit',type: "0", idpe: id},
			dataType: "json",
			success: function(response){
				if(response == true) {
					alert("Cập nhật thành công");
					elem.parent().html('<img class="class-hiden-link" idpr="' + id + '" style="cursor:pointer;" src="../images/lockicon.png" title="Click để hiển thị trên trang chủ" />  <a href="admin.php?content=quanlivay&&as=edit-compare-rate&&id=' + id + '"><img src="../images/icon_detail_cardiscount.png" title="Cập Nhật Thông Tin" /></a>');
				} else {
					alert("Cập nhật thất bại");
				}
			}
		});
	});
	
	$(document).on('click', '.class-hiden-link', function() {
		elem = $(this);
		id = elem.attr('idpr');
		$.ajax({
			type: "POST",
			url: "../src/ajax/ajaxadmincarddetail.php",
			data:{act: 'edit',type: "1", idpe: id},
			dataType: "json",
			success: function(response){
				if(response == true) {
					alert("Cập nhật thành công");
					elem.parent().html('<img class="class-display-link" idpr="' + id + '" style="cursor:pointer;" src="../images/unclockicon.png" title="Click để không cho phép hiển thị" />  <a href="admin.php?content=quanlivay&&as=edit-compare-rate&&id=' + id + '"><img src="../images/icon_detail_cardiscount.png" title="Cập Nhật Thông Tin" /></a>');
				} else {
					alert("Cập nhật thất bại");
				}
			}
		});
	});
	
});
</script>